// 导出 HandlerAdmin 类
export class HandlerAdmin {
    constructor(el) {
        this.handlers = []
        this.el = el
    }

    add(handler) {
        this.handlers.push(handler)
    }

    del(handler) {
        this.handlers = this.handlers.filter((h) => h !== handler)
    }

    dispatch(...args) {
        this.handlers.forEach((handler) => handler.apply(this.el, args))
    }
}

// 导出 AlloyFinger 类
export default class AlloyFinger {
    constructor(el, option) {
        this.element = typeof el === 'string' ? document.querySelector(el) : el
        // 绑定this上下文并添加事件监听
        ;['start', 'move', 'end', 'cancel'].forEach((method) => {
            this[method] = this[method].bind(this)
            this.element.addEventListener(`touch${method}`, this[method], false)
        })

        this.preV = { x: null, y: null }
        this.pinchStartLen = null
        this.zoom = 1
        this.isDoubleTap = false

        // 初始化事件处理函数
        const noop = () => {}
        for (const event of [
            'rotate',
            'touchStart',
            'multipointStart',
            'multipointEnd',
            'pinch',
            'swipe',
            'tap',
            'doubleTap',
            'longTap',
            'singleTap',
            'pressMove',
            'twoFingerPressMove',
            'touchMove',
            'touchEnd',
            'touchCancel'
        ]) {
            this[event] = new HandlerAdmin(this.element)
            this[event].add(option[event] || noop)
        }

        this._cancelAllHandler = this.cancelAll.bind(this)
        window.addEventListener('scroll', this._cancelAllHandler)
    }

    // ... 此处省略其他方法定义，保持原样不变 ...

    // 取消所有事件处理函数并销毁实例
    destroy() {
        ;['start', 'move', 'end', 'cancel'].forEach((method) => {
            this.element.removeEventListener(`touch${method}`, this[method], false)
        })
        window.removeEventListener('scroll', this._cancelAllHandler)

        // 清理事件处理器
        ;[
            'rotate',
            'touchStart',
            'multipointStart',
            'multipointEnd',
            'pinch',
            'swipe',
            'tap',
            'doubleTap',
            'longTap',
            'singleTap',
            'pressMove',
            'twoFingerPressMove',
            'touchMove',
            'touchEnd',
            'touchCancel'
        ].forEach((event) => {
            this[event].del()
        })

        // 清理定时器等资源
        // ... 清理逻辑保持原样不变 ...
    }
}

// 导出 getLen, dot, getAngle, cross, getRotateAngle 等函数，如果需要在外部使用
export { getLen, dot, getAngle, cross, getRotateAngle }
